package com.zjdiepu.www.biz.icbc;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.icbc.YwOrdernoBankMapper;
import com.zjdiepu.www.ibiz.icbc.IYwOrdernoBankService;
import com.zjdiepu.www.model.icbc.dto.YwOrdernoBank;
import com.zjdiepu.www.model.icbc.ext.YwOrdernoBankExt;

/**
 * yw_银行订单号Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 18时05分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwOrdernoBankServiceImpl extends BaseService implements IYwOrdernoBankService {
	public static final Logger logger = LoggerFactory.getLogger(YwOrdernoBankServiceImpl.class);

	@Autowired
	private YwOrdernoBankMapper ywOrdernoBankMapper;
	
	@Override
	public void saveUpdateAndDelete(YwOrdernoBank ywOrdernoBank, OperateType operType) {
		switch (operType) {
		case add:
			ywOrdernoBank.initInsertData(ywOrdernoBank.getModifier(), ywOrdernoBank.getModifierName());
			ywOrdernoBankMapper.insert(ywOrdernoBank);
			break;
		case edit:
			ywOrdernoBank.initUpdateData(ywOrdernoBank.getModifier(), ywOrdernoBank.getModifierName());
			// 影响的记录数
			int updateCount = ywOrdernoBankMapper.updateByPrimaryKeySelective(ywOrdernoBank);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywOrdernoBank.initDeleteData(ywOrdernoBank.getModifier(), ywOrdernoBank.getModifierName());
			// 影响的记录数
			int deleteCount = ywOrdernoBankMapper.updateByPrimaryKeySelective(ywOrdernoBank);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwOrdernoBank> selectByExt(YwOrdernoBankExt ywOrdernoBankExt) {
		return ywOrdernoBankMapper.selectByExt(ywOrdernoBankExt);
	}
	
	@Override
	public int countByExt(YwOrdernoBankExt ywOrdernoBankExt){
		return ywOrdernoBankMapper.countByExt(ywOrdernoBankExt);
	}
	
	@Override
	public YwOrdernoBank selectById(String id) {
		return ywOrdernoBankMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywOrdernoBankMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwOrdernoBank ywOrdernoBank) {
		return ywOrdernoBankMapper.countRefined(ywOrdernoBank);
	}

	@Override
	public YwOrdernoBank selectRefined(YwOrdernoBank ywOrdernoBank) {
		return ywOrdernoBankMapper.selectRefined(ywOrdernoBank);
	}
	
	@Override
	public int deletedRefined(YwOrdernoBank ywOrdernoBank) {
		return ywOrdernoBankMapper.deletedRefined(ywOrdernoBank);
	}

	@Override
	public boolean repeat(YwOrdernoBank ywOrdernoBank) {
		int count = countRefined(ywOrdernoBank);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywOrdernoBank.getId())) {
			return true;
		}
		YwOrdernoBank dbItem = selectRefined(ywOrdernoBank);
		if(null == dbItem) {
			return false;
		}
		return !ywOrdernoBank.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywOrdernoBankMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwOrdernoBank> ywOrdernoBanks) {
		return ywOrdernoBankMapper.insertBatch(ywOrdernoBanks);
	}
}
